package net.thetabx.ftdba.activities;

import java.util.List;

import net.thetabx.ftdba.GlobalData;
import net.thetabx.ftdba.R;
import net.thetabx.ftdba.RStatus;
import net.thetabx.ftdba.Userlist;
import net.thetabx.ftdba.http.HttpManager;
import net.thetabx.ftdba.objects.User;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

public class UserlistActivity extends Activity {
	
    private TextView txtData;
    
    private HttpManager http;
    private Thread userlistRefresher;
    private Boolean ulUpdaterRun = false;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.userlist);
        
        txtData = (TextView)findViewById(R.id.txtUlData);
        
        http = GlobalData.getHttpManager();
    }
    
    @Override
    public void onResume() {
    	super.onResume();
    	if(userlistRefresher != null && userlistRefresher.isAlive())
    		ulUpdaterRun = true;
    	else {
    		initUserlistRefresher();
    		userlistRefresher.start();
    	}
    }
    
    @Override
    public void onPause() {
    	super.onPause();
    	ulUpdaterRun = false;
    }
    
    public void initUserlistRefresher() {
    	userlistRefresher = new Thread(new Runnable() {
            public void run() {
            	ulUpdaterRun = true;
            	Userlist ul = new Userlist();
            	while(ulUpdaterRun) {
            		ul.pullUsers(http);
            		
            		String display = "";
            		if(ul.getStatus() == RStatus.ENDED) {
                		List<User> userList = ul.getUserList();
                		display += userList.size() + " connected\n";
                		for(int i = 0; i < userList.size(); i++) {
                			User u = userList.get(i);
                			display += "[" + u.getHash() + "]" + u.getName() + "\n";
                		}
                	}
                	else
                		display = ul.getStatus().toString();
            		
            		setDataText(display);
            		
            		try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
            	}
            }
        });
    }
    
    public void setDataText(final String str) {
    	txtData.post(new Runnable() {
            public void run() {
            	txtData.setText(str);
            }
        });
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
    	menu.findItem(R.id.menu_userlist).setVisible(false);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.menu_userstat: {
            	finish();
        		return true;
            }
            case R.id.menu_shoutbox: {
            	finish();
        		startActivity(new Intent(this, ShoutboxActivity.class));
        		return true;
            }
            case R.id.menu_torrents: {
            	finish();
        		startActivity(new Intent(this, TorrentsListActivity.class));
        		return true;
            }
            case R.id.menu_options: {
            	finish();
        		startActivity(new Intent(this, OptionsActivity.class));
                return true;
            }
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}
